<template>
    <div class="register-container">
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>用户注册</span>
          </div>
        </template>
        <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-width="100px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="registerForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="registerForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="password" v-model="registerForm.confirmPassword" show-password></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="beizhu">
            <el-input v-model="registerForm.beizhu"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">注册</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script>
  import { ref } from 'vue';
  import { ElMessage } from 'element-plus';
  import axios from '@/utils/request.js'; // 确保路径正确
  
  export default {
    setup() {
      const registerForm = ref({
        username: '',
        password: '',
        confirmPassword: '',
        beizhu: ''
      });
  
      const validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (registerForm.value.confirmPassword !== '') {
            registerFormRef.value.validateField('confirmPassword');
          }
          callback();
        }
      };
  
      const validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== registerForm.value.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
  
      const rules = ref({
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        confirmPassword: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        beizhu: [
          { required: false, message: '请输入备注', trigger: 'blur' } // 备注字段可选
        ]
      });
  
      const registerFormRef = ref(null);
  
      const submitForm = () => {
        registerFormRef.value.validate((valid) => {
          if (valid) {
            axios(
            {
              url:'/register',
              method:'post',
             params:{
              username:registerForm.value.username,
              password:registerForm.value.password
             },
            } )
            .then((response) => {
              if (response.result === 'success') {
                ElMessage.success('注册成功!');
                // 可以在这里添加跳转逻辑，例如跳转到登录页面
                // router.push({ name: 'Login2' });
              } else {
                ElMessage.error('注册失败，请重试');
              }
            })
            .catch((error) => {
              ElMessage.error('注册失败，请重试');
              console.error(error);
            });
          } else {
            ElMessage.error('请检查表单信息!');
            return false;
          }
        });
      };
  
      const resetForm = () => {
        registerFormRef.value.resetFields();
      };
  
      return {
        registerForm,
        rules,
        registerFormRef,
        submitForm,
        resetForm
      };
    }
  };
  </script>
  
  <style scoped>
  .register-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f0f2f5;
  }
  
  .box-card {
    width: 400px;
  }
  </style>